<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use App\Models\Relations\AttachmentTrait;
use App\Models\Relations\AreaTrait;
use App\Models\Relations\CategoryTrait;
use App\Models\Relations\CommonTrait;

class Organization extends Model
{
    //
    use SoftDeletes,AttachmentTrait,AreaTrait,CategoryTrait,CommonTrait;

    protected $table = 'organization';

    protected $dates = ['deleted_at'];

    protected $fillable = ['id','name','cat_id','address', 'telphone', 'breif', 'introduction','activities','province', 'city', 'area', 'show_at','thumb'];


    /**
     * 机构对课程的一对多关系映射
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function allclass() {
        return $this->hasMany('App\Models\Courses', 'org_id','id');
    }
    
    
    /***
     * 机构对课程的一对多关系映射
     */
    public function orcoursed($id)
    {
        return DB::table('courses')->where('org_id','=', $id)->get();
    }
    
    /***
     * 获取机构的分类名称和机构的全部信息
     */
    public function getList($where = '')
    {
        $query = DB::table('organization')->leftJoin('category','organization.cat_id','=','category.cat_id')
                ->select('organization.*','category.cat_name');
        if(!empty($where['cate_id'])){
            $query->where('cat_id','=',$where['cate_id']);
        }
        return $query->lists('organization.id');
    }
}
